package work;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class XmlGenerator {
	private DocumentBuilderFactory factory;
	private DocumentBuilder builder;
	private Document document;
	private Element root;
	private Element entities;
	private Element relations;
	private int entity_count;
	private int relation_count;
	public XmlGenerator() throws ParserConfigurationException{
		factory = DocumentBuilderFactory.newInstance();
		builder= factory.newDocumentBuilder();
		document = builder.newDocument();
		root = document.createElement("EntityExtraction");
		entities = document.createElement("Entities");
		document.appendChild(root);
		root.appendChild(entities);
		relations = document.createElement("Relatons");
		root.appendChild(relations);
		entity_count = 0;
		relation_count = 0;
	}
	public void appendNameEntity(String type, String text, int doc, int start){
		Element entity = document.createElement("Entity");
		entity.setAttribute("type", type);
		Element Text = document.createElement("Text");
		Text.appendChild(document.createTextNode(text));
		entity.appendChild(Text);
		Element Doc = document.createElement("Doc");
		Doc.appendChild(document.createTextNode(String.valueOf(doc)));
		entity.appendChild(Doc);
		Element Start = document.createElement("Start");
		Start.appendChild(document.createTextNode(String.valueOf(start)));
		entity.appendChild(Start);
		entities.appendChild(entity);
		++entity_count;
	}
	public void appendRelation(String type1, String type2, String text1, String text2, int doc, String relation){
		Element re = document.createElement("Relation");
		re.setAttribute("type", type1 + "-" + type2);
		Element Obj1 = document.createElement(type1.toUpperCase());
		Obj1.appendChild(document.createTextNode(text1));
		re.appendChild(Obj1);
		Element Obj2 = document.createElement(type2.toUpperCase());
		Obj2.appendChild(document.createTextNode(text2));
		re.appendChild(Obj2);
		Element Doc = document.createElement("Doc");
		Doc.appendChild(document.createTextNode(String.valueOf(doc)));
		re.appendChild(Doc);
		Element Desc = document.createElement("Desc");
		Desc.appendChild(document.createTextNode(relation));
		re.appendChild(Desc);
		relations.appendChild(re);
		++relation_count;
		
	}
	public void saveXml(String filename) throws FileNotFoundException, TransformerException{
		entities.setAttribute("count", String.valueOf(entity_count));
		relations.setAttribute("count", String.valueOf(relation_count));
		TransformerFactory tf = TransformerFactory.newInstance(); 
		Transformer transformer = tf.newTransformer();
		DOMSource source = new DOMSource(document);
		transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
		PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
		StreamResult result = new StreamResult(pw);
		transformer.transform(source, result);
		pw.close();
	}
}
